package blazingbubble.graphics.lobby;

import moltencore.game.Game;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class CreateGameOptionsWindow extends JDialog implements ActionListener, ChangeListener {
	private static final long serialVersionUID = 5498815399639835777L;

	private static final String WINDOW_TITLE = "Game Options";

	private static final int MIN_BUBBLES_COUNT = 1;
	private static final int MAX_BUBBLES_COUNT = 8;
	private static final int MIN_PLAYERS_COUNT = 2;
	private static final int MAX_PLAYERS_COUNT = 5;

	private static final String GAMENAME_LABEL_TEXT = new String("Game Name:");
	private static final String GAMEMODE_LABEL_TEXT = new String("Game Mode:");
	private static final String MAXPLAYERS_LABEL_TEXT = new String("Max. Players:");
	private static final String NORMALBUBBLESCOUNT_LABEL_TEXT = new String("Normal Bubbles Colors:");
	private static final String GHOSTBUBBLESENABLED_LABEL_TEXT = new String("Ghost Bubbles Enabled:");
	private static final String RAINBOWBUBBLES_ENABLED_LABEL_TEXT = new String("Rainbow Bubbles Enabled:");
	private static final String BLAZINGBUBBLES_ENABLED_LABEL_TEXT = new String("Blazing Bubbles Enabled:");
	private static final String GRAVITY_ENABLED_LABEL_TEXT = new String("Gravity Enabled:");
	private static final String WIND_ENABLED_LABEL_TEXT = new String("Wind Enabled:");
	private static final String FIELDFLIPPED_ENABLED_LABEL_TEXT = new String("Flipped Field Enabled:");
	private static final String TEAMS_ENABLED_LABEL_TEXT = new String("Teams Enabled:");

	private JLabel gameNameLabel = new JLabel(GAMENAME_LABEL_TEXT);
	private JLabel gameModeLabel = new JLabel(GAMEMODE_LABEL_TEXT);
	private JLabel maxPlayersLabel = new JLabel(MAXPLAYERS_LABEL_TEXT);
	private JLabel normalBubblesCountLabel = new JLabel(NORMALBUBBLESCOUNT_LABEL_TEXT);
	private JLabel ghostBubblesEnabledLabel = new JLabel(GHOSTBUBBLESENABLED_LABEL_TEXT);
	private JLabel rainbowBubblesEnabledLabel = new JLabel(RAINBOWBUBBLES_ENABLED_LABEL_TEXT);
	private JLabel blazingBubblesEnabledLabel = new JLabel(BLAZINGBUBBLES_ENABLED_LABEL_TEXT);
	private JLabel gravityEnabledLabel = new JLabel(GRAVITY_ENABLED_LABEL_TEXT);
	private JLabel windEnabledLabel = new JLabel(WIND_ENABLED_LABEL_TEXT);
	private JLabel flippedFieldEnabledLabel = new JLabel(FIELDFLIPPED_ENABLED_LABEL_TEXT);
	private JLabel teamsEnabledLabel = new JLabel(TEAMS_ENABLED_LABEL_TEXT);

	private JTextField gameNameField = new JTextField();
	private JComboBox gameModeComboBox = new JComboBox();
	private JSlider maxPlayersSlider = new JSlider(MIN_PLAYERS_COUNT, MAX_PLAYERS_COUNT);
	private JTextField maxPlayersSelectedField = new JTextField();
	private JSlider normalBubblesCountSlider = new JSlider(MIN_BUBBLES_COUNT, MAX_BUBBLES_COUNT);
	private JTextField normalBubblesSelectedField = new JTextField();
	private JCheckBox ghostBubblesEnabledBox = new JCheckBox();
	private JCheckBox rainbowBubblesEnabledBox = new JCheckBox();
	private JCheckBox blazingBubblesEnabledBox = new JCheckBox();
	private JCheckBox gravityEnabledBox = new JCheckBox();
	private JCheckBox windEnabledBox = new JCheckBox();
	private JCheckBox flippedFieldEnabledBox = new JCheckBox();
	private JCheckBox teamsEnabledBox = new JCheckBox();

	private JButton startButton = new JButton("Start Game");
	private JButton cancelButton = new JButton("Cancel");

	private GridBagConstraints constraints = new GridBagConstraints();

	private LobbyScreen lobbyScreen = null;

	public CreateGameOptionsWindow(Frame owner, LobbyScreen lobbyScreen){
		super(owner, WINDOW_TITLE, true);
		this.lobbyScreen = lobbyScreen;
		setUp();
	}

	private void setUp(){
		this.setLayout(new GridBagLayout());
		this.setSize(420, 320);

		gameModeComboBox.addItem(Game.Mode.DeathMatch);
		gameModeComboBox.addItem(Game.Mode.TimeAttack);
		gameModeComboBox.addItem(Game.Mode.ShootEmUp);

		startButton.addActionListener(this);
		cancelButton.addActionListener(this);

		normalBubblesCountSlider.addChangeListener(this);
		maxPlayersSlider.addChangeListener(this);
		normalBubblesCountSlider.setSnapToTicks(true);
		maxPlayersSlider.setSnapToTicks(true);
		maxPlayersSlider.setValue(2);

		normalBubblesSelectedField.setEditable(false);
		normalBubblesSelectedField.setText(Integer.toString(normalBubblesCountSlider.getValue()));
		maxPlayersSelectedField.setEditable(false);
		maxPlayersSelectedField.setText(Integer.toString(maxPlayersSlider.getValue()));


		constraints.gridx = 0;
		constraints.gridy = 0;
		this.add(gameNameLabel, constraints);

		constraints.gridy = 1;
		this.add(gameModeLabel, constraints);

		constraints.gridy = 2;
		this.add(maxPlayersLabel, constraints);

		constraints.gridy = 3;
		this.add(normalBubblesCountLabel, constraints);

		constraints.gridy = 4;
		this.add(ghostBubblesEnabledLabel, constraints);

		constraints.gridy = 5;
		this.add(rainbowBubblesEnabledLabel, constraints);

		constraints.gridy = 6;
		this.add(blazingBubblesEnabledLabel, constraints);

		constraints.gridy = 7;
		this.add(gravityEnabledLabel, constraints);

		constraints.gridy = 8;
		this.add(windEnabledLabel, constraints);

		constraints.gridy = 9;
		this.add(flippedFieldEnabledLabel, constraints);

		constraints.gridy = 10;
		this.add(teamsEnabledLabel, constraints);

		constraints.gridy = 0;
		constraints.gridx = 1;
		constraints.fill = constraints.HORIZONTAL;
		this.add(gameNameField, constraints);

		constraints.gridy = 1;
		constraints.fill = constraints.NONE;
		this.add(gameModeComboBox, constraints);

		constraints.gridy = 2;
		this.add(maxPlayersSlider, constraints);

		constraints.gridx = 2;
		this.add(maxPlayersSelectedField, constraints);

		constraints.gridx = 1;
		constraints.gridy = 3;
		this.add(normalBubblesCountSlider, constraints);

		constraints.gridx = 2;
		this.add(normalBubblesSelectedField, constraints);

		constraints.gridx = 1;
		constraints.gridy = 4;
		this.add(ghostBubblesEnabledBox, constraints);

		constraints.gridy = 5;
		this.add(rainbowBubblesEnabledBox, constraints);

		constraints.gridy = 6;
		this.add(blazingBubblesEnabledBox, constraints);

		constraints.gridy = 7;
		this.add(gravityEnabledBox, constraints);

		constraints.gridy = 8;
		this.add(windEnabledBox, constraints);

		constraints.gridy = 9;
		this.add(flippedFieldEnabledBox, constraints);

		constraints.gridy = 10;
		this.add(teamsEnabledBox, constraints);

		constraints.gridx = 0;
		constraints.gridy = 11;
		this.add(cancelButton, constraints);

		constraints.gridx = 1;
		constraints.gridy = 11;
		this.add(startButton, constraints);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(startButton)){
			lobbyScreen.startNewGame(
					gameNameField.getText(),
					(Game.Mode)gameModeComboBox.getSelectedItem(),
					maxPlayersSlider.getValue(),
					normalBubblesCountSlider.getValue(),
					ghostBubblesEnabledBox.isSelected(),
					rainbowBubblesEnabledBox.isSelected(),
					blazingBubblesEnabledBox.isSelected(),
					gravityEnabledBox.isSelected(),
					windEnabledBox.isSelected(),
					flippedFieldEnabledBox.isSelected(),
					teamsEnabledBox.isSelected()
					);
		}
		this.setVisible(false);
	}

	@Override
	public void stateChanged(ChangeEvent event) {
		if (event.getSource().equals(normalBubblesCountSlider)){
			normalBubblesSelectedField.setText(Integer.toString(normalBubblesCountSlider.getValue()));
		}
		if (event.getSource().equals(maxPlayersSlider)){
			maxPlayersSelectedField.setText(Integer.toString(maxPlayersSlider.getValue()));
		}
	}
}
